
#include "ZsLog.h"

static std::string g_log_path;
FileChannel* LogOutPut::FileChannel_ = nullptr;
ConsoleChannel* LogOutPut::ConsoleChannel_ = nullptr;
FormattingChannel* LogOutPut::FormattingChannel_ = nullptr;
//日志输出格式
const char* log_format = "%Y-%m-%d %H:%M:%S [TID:%J] %p %t";
const char* logger = "ZsLog";
void zs::SetLogPath(const std::string &path)
{
    g_log_path = path;
    // 创建格式化通道，使用PatternFormatter对日志消息进行格式化
    auto* pPatternFormatter = new PatternFormatter;
    pPatternFormatter->setProperty("pattern", log_format);
    if (g_log_path.empty())
    {
        auto* pConsoleChannel = new ConsoleChannel;
        LogOutPut::set_console_channel(pConsoleChannel);
        LogOutPut::set_formatting_channel(new FormattingChannel(pPatternFormatter, pConsoleChannel));
    }
    else
    {
        auto *pFileChannel = new FileChannel(g_log_path);
        pFileChannel->setProperty("rotation", "40 M");
        LogOutPut::set_file_channel(pFileChannel);
        LogOutPut::set_formatting_channel(new FormattingChannel(pPatternFormatter, pFileChannel));
    }
    // 设置日志通道
    Logger::root().setChannel(LogOutPut::get_formatting_channel());

    // 设置日志级别
    Logger::root().setLevel(Poco::Message::PRIO_INFORMATION);
}

LogOutPut::LogOutPut(BaseName filename, int line)
{
    logStream_ << filename.basename_ << ":" << line << " - ";
}

LogOutPut::LogOutPut(BaseName filename, int line, const char *function_name)
{
    logStream_ << filename.basename_ << ":" << line << " FUNC:" << function_name << " - ";
}

LogOutPut::~LogOutPut()
{
    end_line();
}

void LogOutPut::set_log_out_func(LogOutPut::LogOutFunc func)
{

}

void LogOutPut::set_file_channel(FileChannel * fileChannel)
{
    FileChannel_ = fileChannel;
}

void LogOutPut::set_console_channel(ConsoleChannel * consoleChannel)
{
    ConsoleChannel_ = consoleChannel;
}

void LogOutPut::set_formatting_channel(FormattingChannel * formattingChannel)
{
    FormattingChannel_ = formattingChannel;
}

void LogOutPut::end_line()
{
    logStream_ << std::endl;
}
